//
//  AttachView.swift
//  Dynamic
//
//  Created by Yinyin Zeng on 27/5/20.
//  Copyright © 2020 MaoYeYeDeJiangCuCha. All rights reserved.
//

import UIKit

class AttachView: UIView {
    

     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
        let bezierPath = UIBezierPath()
        bezierPath.move(to:startPoint)
        bezierPath.addLine(to: endPoint)
        bezierPath.stroke()
        
        let bezierPathTwo = UIBezierPath()
        bezierPathTwo.move(to:startPointTwo)
        bezierPathTwo.addLine(to: endPointTwo)
        bezierPathTwo.stroke()
        
     }
    
    let squareView = UIImageView()
    let squareViewTwo = UIImageView()
    let squareViewThree = UIImageView()
    var animator = UIDynamicAnimator()//需要强引用
    var attach :UIAttachmentBehavior?//强附着
    var attahWithItem:UIAttachmentBehavior?
    var startPoint = CGPoint()
    var endPoint = CGPoint()
    
    var startPointTwo = CGPoint()
    var endPointTwo = CGPoint()
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        self.backgroundColor = .white
        squareView.image = UIImage(named: "Box1")
        squareView.frame = CGRect(x: 25, y: 100, width: 100, height: 100)
        self.addSubview(squareView)
        
        squareViewTwo.image = UIImage(named: "Box2")
        squareViewTwo.frame = CGRect(x: 175, y: 100, width: 100, height: 100)
        self.addSubview(squareViewTwo)
        
        squareViewThree.image = UIImage(named: "Box2")
        squareViewThree.frame = CGRect(x: 300, y: 100, width: 100, height: 100)
        self.addSubview(squareViewThree)
        
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let currentPoint = touch.location(in: touch.view)
            //创建动画对象
            animator = UIDynamicAnimator(referenceView: self)
            //创建动画行为
            self.attach = UIAttachmentBehavior(item: self.squareView, attachedToAnchor: currentPoint)
            //默认刚性附着 - 固定长度
//            self.attach?.length = 10
            //弹性附着 - 设置以下两个参数
            self.attach?.damping = 0.5 //减震幅度
            self.attach?.frequency = 0.5 //频率
            
            self.attahWithItem = UIAttachmentBehavior(item: squareViewTwo, attachedTo: squareViewThree)
            
            //重力
            let gravity = UIGravityBehavior(items: [self.squareView,self.squareViewThree])
            //把行为添加到动画中
            
            let collision = UICollisionBehavior(items: [self.squareView,self.squareViewTwo,self.squareViewThree])
            collision.translatesReferenceBoundsIntoBoundary = true
            
            animator.addBehavior(self.attach!)
            animator.addBehavior(gravity)
            animator.addBehavior(collision)
            animator.addBehavior(attahWithItem!)
            
            
            self.attach?.action = {
                self.startPoint = self.squareView.center
                self.endPoint = currentPoint
                self.setNeedsDisplay()
            }
            
            self.attahWithItem?.action = {
                self.startPointTwo = self.squareViewTwo.center
                self.endPointTwo = self.squareViewThree.center
                self.setNeedsDisplay()
            }
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let currentPoint = touch.location(in: touch.view)
            self.attach!.anchorPoint = currentPoint
            //监听变化
            self.attach?.action = {
                self.startPoint = self.squareView.center
                self.endPoint = currentPoint
                self.setNeedsDisplay()
            }
            
            
            
        }
    }
}
